import $ from 'jquery';
import {Message, MessageBox} from 'element-ui';
import store from '@/store';
import constant from '@/utils/constant';
import appName from '@/api/appName';
import {getToken} from '@/utils/auth';

const uwLoadJs = {
  getUrl: function(key){
    let token = getToken(constant.TOKEN);
    let urls =JSON.parse(sessionStorage.getItem(constant.APP_CONTEXT_URL));
    if(urls==null && sessionStorage.getItem(token)){
      urls =JSON.parse(sessionStorage.getItem(token)).appContextUrl;
    }
    let url = '';
    if(urls!="" && urls!=undefined && urls!=null){
      if(key=='PAS'){
        url = urls.PAS;
      }else if(key=='CWRS'){
        url = urls.CWRS;
      }else if(key=='CITICGIS'){
        url = urls.CITICGIS;
      }
    }
    return url;
  },
  getRootPath: function(){
    //获取当前网址
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    let url = localhostPaht+projectName;
    return url;
  },
  doLogin: function(xhr,url){
    console.log("dologin:"+constant.loginUrl)
    console.log(xhr)
    if(xhr.status=="401"){
      MessageBox.confirm('您登录已失效，可以取消继续留在该页面，或者重新登录', '确定登出', {
        confirmButtonText: '重新登录',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        store.dispatch('LogOut', {});
        window.sessionStorage.setItem('tagActive','')
        window.location.href= constant.loginUrl;
      });
    }else{
      let appName = '';
      if(url.indexOf('/ilmp/')>-1){
        let start = url.substring(6,url.length);
        let end = start.substring(0,start.indexOf('/'));
        appName = end;
      }else{
        if(url.indexOf('CITICGIS')>-1){
          appName = 'CITICGIS';
        }
      }
      Message({
        message: '请求微服务【'+appName+'】异常,请求路径:【'+url+'】',
        type: 'error',
        duration: 15000,
        showClose: true,
      });
    }
  },

  ajaxRequest: function(params,url){
    let data = null;
    $.ajax({
      headers: {
        Authorization: getToken(constant.TOKEN) || ''
      },
      url: url,//url路径
      type: 'POST', //GET
      async: false, //或false,是否异步
      data: JSON.stringify(params),
      contentType: "application/json;charset=UTF-8",
      success: function (res, textStatus, jqXHR) {
        if('40101'==res.resultCode){
          MessageBox.confirm('您登录已失效，可以取消继续留在该页面，或者重新登录', '确定登出', {
            confirmButtonText: '重新登录',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            store.dispatch('LogOut', {});
            window.sessionStorage.setItem('tagActive','')
            window.location.href= constant.loginUrl;
          });
        }else {
          data = res;
        }
      },
      error: function (xhr, textStatus, errorThrown) {
        uwLoadJs.doLogin(xhr,url);
      }
    })

    return data;
  },

  checkSessionChange:function(params){
    let url = appName.NEWUNDR + '/unipnd/nucleusNote/checkSessionChange';
    return uwLoadJs.ajaxRequest(params,url);
  },

  findEuaapkyByApplnum:function(params){
    let url = appName.NEWUNDR + '/unipnd/nucleusNote/findEuaapkyByApplnum';
    return uwLoadJs.ajaxRequest(params,url);
  },

  fGetrelmonth:function(params){
    let url = appName.NEWUNDR + '/unipnd/nucleusNote/fGetrelmonth';
    return uwLoadJs.ajaxRequest(params,url);
  },

  findRmblphoneFromCA:function(params){
    let url = appName.NEWUNDR + '/unipnd/nucleusNote/findRmblphoneFromCA';
    return uwLoadJs.ajaxRequest(params,url);
  },
  findPhoneFromCA:function(params){
    let url = appName.NEWUNDR + '/unipnd/nucleusNote/findPhoneFromCA';
    return uwLoadJs.ajaxRequest(params,url);
  },
  //照会代码检查
  checkPncodeAndZresnpdFromEufapky:function(params){
    let url = appName.NEWUNDR + '/unipnd/nucleusNote/checkPncodeAndZresnpdFromEufapky';
    return uwLoadJs.ajaxRequest(params,url);
  },
  deteRoll:function(params){
    let url = appName.NEWUNDR + '/unipnd/nucleusNote/deteRoll';
    return uwLoadJs.ajaxRequest(params,url);
  },
  callEuis019:function(params){
    let url = appName.NEWUNDR + '/unipnd/premium/callEuis019';
    return uwLoadJs.ajaxRequest(params,url);
  },
  findAgcomrtByUwlevel:function(params){
    let url = appName.NEWUNDR + '/unipnd/premium/findAgcomrtByUwlevel';
    return uwLoadJs.ajaxRequest(params,url);
  },
  findEufupkyCount:function(params){
    let url = appName.NEWUNDR + '/unipnd/premium/findEufupkyCount';
    return uwLoadJs.ajaxRequest(params,url);
  },
  countEuhcByPncodeAndFlag01:function(params){
    let url = appName.NEWUNDR+'/unipnd/nucleusNote/countEuhcByPncodeAndFlag01';
    return uwLoadJs.ajaxRequest(params,url);
  }


}

export default uwLoadJs;